/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.grantOfficer;

import researchgrants.parts.LoggedData.LoggedDataOutSerializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.LoggedData.panels.lists.ListableLoggedData;
import researchgrants.parts.db.Db;

/**
 * Represent a GrantOfficer, which is a field within a GrantRequest.
 * 
 * The class is LoggedDataOutSerializable to allow it to be saved implicitly through panels
 * The class is ListableLoggedData to allow it to populate lists (LoggedDataPanelsList)
 * @author DOStudent1
 */
public class GrantOfficer implements LoggedDataOutSerializable, ListableLoggedData{

    // Retrieves a list of all GrantOfficers
    static public List<GrantOfficer> getAllGrantOfficers() {
        List<GrantOfficer> allGrantOfficers=new LinkedList<GrantOfficer>();

        try {
            Connection conn=Db.openDbConnection();
            PreparedStatement stat = Db.createPreparedStatement(conn, "SELECT GrantOfficerID FROM TblGrantOfficers ORDER BY GrantOfficerID");
            stat.executeQuery();
            ResultSet rs=stat.getResultSet();
            while (rs.next()) {
                allGrantOfficers.add(GrantOfficer.getById((Integer)Db.getColumn(rs, 1)));
            }
            rs.close();
            stat.close();
            Db.closeDbConnection(conn);
            
        } catch (SQLException ex) {
            Logger.getLogger(GrantOfficer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            throw new IllegalArgumentException("Problem with Data: " + ex.getLocalizedMessage());
        }
        
        return(allGrantOfficers);
    }
    
    public static GrantOfficer getById(int grantOfficerId) {
        return(new GrantOfficer(grantOfficerId));
    }

    ///////////////////////////////////////
    // The fields of the GrantOfficer
    ///////////////////////////////////////
    
    private int id;
    private String name;

    // private constructor
    private GrantOfficer(int grantOfficerId) {
        id=grantOfficerId;
        syncDataFromDb();
    }

    // populate the fields from the database
    private void syncDataFromDb() {
        try {
            Connection conn=Db.openDbConnection();
            PreparedStatement stat = Db.createPreparedStatement(conn, "SELECT GrantOfficerName FROM TblGrantOfficers Where GrantOfficerID=?");
            stat.setInt(1, id);
            stat.executeQuery();
            ResultSet rs=stat.getResultSet();
            Db.moveToFirstLine(rs);
            
            name=(String) Db.getColumn(rs, 1);
            
            rs.close();
            stat.close();
            Db.closeDbConnection(conn);
            
        } catch (SQLException ex) {
            Logger.getLogger(GrantOfficer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            throw new IllegalArgumentException("Problem with Data: " + ex.getLocalizedMessage());
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final GrantOfficer other = (GrantOfficer) obj;
        return (this.id==other.id);
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 29 * hash + this.id;
        return hash;
    }

    //////////////////////////////////////////////
    // Allows access to the Grant Officer values
    //////////////////////////////////////////////

    public int getId() {
        return(id);
    }
    
    public String getName() {
        return(name);
    }
    
    
    // Serializable like methods for LoggedData
    
    @Override
    public String toLoggedData() {
        return(Integer.toString(id));
    }
    
//    public static GrantOfficer fromLoggedData(String loggedData) {
//        return(GrantOfficer.getById(Integer.parseInt(loggedData)));
//    }

    // Serializable like methods for LoggedData lists
    public String toListValue() {
        return(name);
    }

    public String toListId() {
        return(Integer.toString(id));
    }
    
    public static GrantOfficer fromListId(String listId) {
        return(GrantOfficer.getById(Integer.parseInt(listId)));
    }

    public void fromLoggedData(String loggedDataString) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
